﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Kaiser.Models;
using Kaiser.Modules.Cubic.DomainModel.Product;
using Kaiser.Modules.Cubic.Repositories;
using Kaiser.Modules.Cubic.ViewModel;

namespace Kaiser.Modules.Cubic.Services.Impl
{
    public class ProductCategoryService : IProductCategoryService
    {
        private readonly IProductCategoryRepositorie _productCategoryRepositorie;

        public ProductCategoryService(IProductCategoryRepositorie productCategoryRepositorie)
        {
            _productCategoryRepositorie = productCategoryRepositorie;
        }

        public Models.DataGrid<DomainModel.Product.ProductCategory> SearchProductCategory(ViewModel.ProductCategorySearch search, Models.PagingInfo pageinfo)
        {
            try
            {
                return _productCategoryRepositorie.SearchProductCategory(search, pageinfo);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public Models.DataGrid<DomainModel.Product.ProductCategory> GetProductCategoryAll(ViewModel.ProductCategorySearch search, Models.PagingInfo pageinfo)
        {
            try
            {
                return _productCategoryRepositorie.GetProductCategoryAll(search, pageinfo);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void Insert(DomainModel.Product.ProductCategory model)
        {
            try
            {
                _productCategoryRepositorie.Insert(model);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void Update(DomainModel.Product.ProductCategory model)
        {
            try
            {
                _productCategoryRepositorie.Update(model);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Delete(object primaryId)
        {
            try
            {
                return _productCategoryRepositorie.Delete(primaryId);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public DomainModel.Product.ProductCategory Get(object primaryId)
        {
            try
            {
                return _productCategoryRepositorie.Get(primaryId);
            }
            catch (Exception)
            {
                throw;
            }
        }


        public int DeleteList(IList<string> ids)
        {
            try
            {
                return _productCategoryRepositorie.DeleteList(ids);
            }
            catch (Exception)
            {
                throw;
            }
        }


        public IList<DomainModel.Product.ProductCategory> GetCategoryTreeList()
        {
            try
            {
                var list = _productCategoryRepositorie.SearchProductCategory(new ProductCategorySearch(), new PagingInfo(0, 100000));
                IList<DomainModel.Product.ProductCategory> resultlist = new List<ProductCategory>();

                var rows = list.rows.Where(x => x.ParentId == 0);
                foreach (var item in rows)
                {
                    AddTreeNodes(item, list.rows);
                    resultlist.Add(item);
                }
                return resultlist;
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void AddTreeNodes(ProductCategory item, IList<ProductCategory> list)
        {
            var childrenList = list.Where(p => item.Id != -1 && item.Id == p.ParentId);
            if (childrenList.Count() < 0)
            {
                return;
            }
            foreach (var category in childrenList)
            {
                AddTreeNodes(category, list);
                item.Categories.Add(category);
            }
        }
    }
}
